//
//  HWCloseGroupView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/1/25.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWCloseGroupView: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    @objc var groupId = ""
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var sureButton:UIButton!
    var cancelButton:UIButton!
    
    @objc var close:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
extension HWCloseGroupView {
    private func initUI() -> Void {
        type = MMPopupType.alert
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 16*widthScale
        addSubview(bgView)
        
        titleLabel = HWLabel.createBoldLabel(title: "温馨提示", color: "#444444", font: 20*widthScale)
        bgView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "退出后，将不再接收该群聊信息", color: "#444444", font: 14*widthScale)
        bgView.addSubview(contentLabel)
        
        sureButton = UIButton(type: .custom)
        sureButton.frame = CGRect(x: 0, y: 0, width: 291*widthScale, height: 44*widthScale)
        sureButton.setTitle("确定", for: .normal)
        sureButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        sureButton.setTitleColor(.white, for: .normal)
        sureButton.layer.cornerRadius = 22*widthScale
        sureButton.layer.masksToBounds = true
        sureButton.addTarget(self, action: #selector(sureButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(sureButton)
        
        UIColor.gradientColor(view: sureButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        cancelButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        cancelButton.layer.cornerRadius = 22*widthScale
        cancelButton.addTarget(self, action: #selector(cancelButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(cancelButton)
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(22*widthScale)
            make.right.equalTo(-22*widthScale)
            make.centerY.equalToSuperview()
            make.height.equalTo(222*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(30*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(10*widthScale)
        }
        
        sureButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom).offset(30*widthScale)
            make.width.equalTo(291*widthScale)
            make.height.equalTo(44*widthScale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(sureButton.snp.bottom).offset(20*widthScale)
        }
    }
}
extension HWCloseGroupView {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        
        hide()
        
    }
    
    @objc func cancelButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
    @objc func sureButtonClick(_ button:UIButton) -> Void {
        HWHomeHttpManager().gameCloseGroup(groupId: self.groupId) { close in
            self.close?()
            self.hide()
        } failure: { error in
            
        }

    }
    
}
